package com.csliubo.eclipse.plugin.sqlgenerator.util;

import com.csliubo.eclipse.plugin.sqlgenerator.core.Config;

/**
 * 字符串工具类
 * 
 * @author liubo.cs@hotmail.com
 * @version 1.0
 * @date 2011-3-5
 */
public class StringUtils {

    /**
     * 是否为空串，包括空白字符串
     * 
     * @return
     */
    public static boolean isBlank(String str) {
        if ((null == str) || (0 == str.length())) {
            return true;
        }
        char[] chars = str.toCharArray();
        for (char ch : chars) {
            if (!Character.isWhitespace(ch)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否相等
     * 
     * @return
     */
    public static boolean equals(String str1, String str2) {
        return null == str1 ? null == str2 : str1.equals(str2);
    }

    /**
     * @param str
     * @param encloser
     * @return
     */
    public static String enclose(String str, String encloser) {
        StringBuilder sb = new StringBuilder();
        sb.append(encloser).append(str).append(encloser);
        return sb.toString();
    }

    private final static char TAB = '\t';

    /**
     * 填充tab符，在写入wiki的时候会用到
     * @param str
     * @param length
     * @return
     */
    public static String fillStrWithTab(String str, int length) {
        if (null == str) {
            str = " ";
        }
        int tabLength = Config.getTabSize();
        int tabLengthMask = 0xffff - tabLength + 1;
        // 将length向上取Tab的整数倍
        if ((length % tabLength) != 0) {
            length = (length & tabLengthMask) + tabLength;
        }
        int needFillLen = length - str.length();
        if (needFillLen <= 0) {
            return str;
        }
        int tabNum = needFillLen / tabLength;
        
        if (needFillLen % tabLength != 0) {
            tabNum += 1;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        for (int i = 0; i < tabNum; i++) {
            sb.append(TAB);
        }
        return sb.toString();
    }
}
